package com.sasu.university_data.controller.after;

import com.sasu.university_data.entity.Area;
import com.sasu.university_data.service.AreaService;
import com.sasu.university_data.util.ResponseCode;
import com.sasu.university_data.util.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "位置功能接口")
@RestController
@RequestMapping("/area")
public class AreaController {

    @Resource
    private AreaService areaService;

    @ApiOperation("分页查询数据")
    @GetMapping("/select")
    public ResponseData selectByPage( Integer aId, Integer page, Integer pageSize){

        List<Area> areas = areaService.selectByPage(aId, page, pageSize);
        if (areas==null){
            return new ResponseData(ResponseCode.NULL_DATA);
        }

        long count = areaService.count(aId);

        return new ResponseData(ResponseCode.SUCCESS,areas,count);
    }

    /**
     * 新增数据
     *
     * @param area 实例对象
     * @return 实例对象
     */
    @ApiOperation("新增数据")
    @PostMapping("/add")
    public ResponseData add( @RequestBody Area area){

        if (!areaService.insert(area)){

            return new ResponseData(ResponseCode.ADD_FAILED);
        }

        long count = areaService.count(null);

        return new ResponseData(ResponseCode.SUCCESS,"success",count);
    }

    /**
     * 更新数据
     *
     * @param area 实例对象
     * @return 实例对象
     */
    @ApiOperation("更新数据")
    @PostMapping("/update")
    public ResponseData edit(@RequestBody Area area){

        if (!areaService.update(area)){
            return new ResponseData(ResponseCode.UPDATE_FAILED);
        }

        long count = areaService.count(null);
        return new ResponseData(ResponseCode.SUCCESS,"success",count);
    }

    /**
     * 通过主键删除数据
     *
     * @param atId 主键
     * @return 是否成功
     */
    @ApiOperation("通过主键删除数据")
    @PostMapping("/delete")
    public ResponseData deleteById(Integer atId){

        if (!areaService.deleteById(atId)){
            return new ResponseData(ResponseCode.DELETE_FAILED);
        }

        long count = areaService.count(null);
        return new ResponseData(ResponseCode.SUCCESS,"success",count);
    }


    /***
     * 通过id批量删除数据
     * @param ids
     * @return
     */
    @ApiOperation("通过id批量删除数据")
    @PostMapping("/lotDelete")
    public ResponseData deleteByIds(int[] ids){

        if (!areaService.deleteByIds(ids)){
            return new ResponseData(ResponseCode.DELETE_FAILED);
        }

        long count = areaService.count(null);
        return new ResponseData(ResponseCode.SUCCESS,"success",count);
    }
}
